﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using MultiResolutioning.Algorithms;
using MultiResolutioning.Files;
using MultiResolutioning.Data;

namespace WebAppClient
{
	public partial class _Default : Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			string path = Context.Server.MapPath("OryginalMeshes/");

		}

		protected void Melax_OnClick(object sender, EventArgs e)
		{
			MelaxSimplifier melax = new MelaxSimplifier();
			//PLYMeshFile loader = new PLYMeshFile();
			Stopwatch sw = new Stopwatch();
			int iterations = int.Parse(this.tbIterations.Text);
			string tim;
			string path;
			string dir = @"d:\Jacek\Szkoła\Studia\Magisterka\";
			string file = Request["file"];
			if (file != null)
			{
				path = dir + file;
			}
			else
			{
				path = dir + "bun_zipper_res4.ply";
			}
			sw.Start();
			MeshData mesh = PLYMeshFile.LoadFromPLYFile(path);
			sw.Stop();
			this.lblTest.Text = "Load: " + sw.Elapsed.ToString();
			sw.Reset();
			ProgressiveMesh pMesh = new ProgressiveMesh(mesh, iterations);
			sw.Start();

			melax.Simplify(pMesh, iterations, out tim);

			PLYMeshFile.SaveToPLYFile(pMesh.mesh, "temp.ply");
			pMesh.DoSplits(iterations);
			//loader.SaveProgressive(pMesh);
			
			sw.Stop();

			this.lblTest.Text += " Calculating: " + sw.Elapsed.ToString();

			sw.Reset();
			sw.Start();

			PLYMeshFile.SaveToPLYFile(mesh, "tempo.ply");

			sw.Stop();

			this.lblTest.Text += " Save: " + sw.Elapsed.ToString();

			sw.Reset();
			sw.Start();

			sw.Stop();

			this.lblTest.Text += " Removing: " + sw.Elapsed.ToString();
			this.lblTest.Text += " " +tim;
		}

		protected void QEM_OnClick(object sender, EventArgs e)
		{
			QEMSimplifier qem = new QEMSimplifier();
			//PLYMeshFile loader = new PLYMeshFile();
			Stopwatch sw = new Stopwatch();

			
			string tim;
			string path;
			string dir = @"d:\Jacek\Szkoła\Studia\Magisterka\";
			string file = Request["file"];
			if (file != null)
			{
				path = dir + file;
			}
			else
			{
				path = dir + "bun_zipper_res4.ply";
			}
			sw.Start();
			MeshData mesh = PLYMeshFile.LoadFromPLYFile(path);

			sw.Stop();
			ProgressiveMesh pMesh = new ProgressiveMesh(mesh, int.Parse(this.tbIterations.Text));
			this.lblTest.Text = "Load: " + sw.Elapsed.ToString();
			sw.Reset();

			sw.Start();

			qem.Simplify(pMesh, int.Parse(this.tbIterations.Text), out tim);
			PLYMeshFile.SaveToPLYFile(pMesh.mesh, "temp.ply");
			pMesh.DoSplits(int.Parse(this.tbIterations.Text));
			sw.Stop();

			this.lblTest.Text += " Calculating: " + sw.Elapsed.ToString();

			sw.Reset();
			sw.Start();

			PLYMeshFile.SaveToPLYFile(pMesh.mesh, "tempo.ply");

			sw.Stop();

			this.lblTest.Text += " Save: " + sw.Elapsed.ToString();

			sw.Reset();
			sw.Start();

			sw.Stop();

			this.lblTest.Text += " Removing: " + sw.Elapsed.ToString();
			this.lblTest.Text += " " + tim;
		}
	}
}